package cn.shyouchi.wuguanjia.android.ui.meetting.feedback;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.meet.MeetApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.matter.MatterMeet;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FeedbackMeetDetailActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.img_header)
    ImageView mImgHeader;
    @BindView(R.id.tv_person_name)
    TextView mTvPersonName;
    @BindView(R.id.tv_apply_time)
    TextView mTvApplyTime;
    @BindView(R.id.tv_meet_name)
    TextView mTvMeetName;
    @BindView(R.id.tv_meet_time)
    TextView mTvMeetTime;
    @BindView(R.id.tv_meet_addr)
    TextView mTvMeetAddr;
    @BindView(R.id.tv_meet_rom)
    TextView mTvMeetRom;
    @BindView(R.id.tv_meet_holder)
    TextView mTvMeetHolder;
    @BindView(R.id.tv_meet_recorder)
    TextView mTvMeetRecorder;
    @BindView(R.id.tv_meet_purpose)
    TextView mTvMeetPurpose;
    @BindView(R.id.textView4)
    TextView mTextView4;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R.id.tv_meet_content)
    TextView mTvMeetContent;
    @BindView(R.id.tv_meet_apply_res)
    TextView mTvMeetApplyRes;

    MeetApi mMeetApi;

    int roomId;
    @BindView(R.id.img_apply_header)
    ImageView mImgApplyHeader;
    @BindView(R.id.tv_apply_person_name)
    TextView mTvApplyPersonName;
    @BindView(R.id.tv_apply_person_msg)
    TextView mTvApplyPersonMsg;
    @BindView(R.id.item_apply)
    LinearLayout mItemApply;
    @BindView(R.id.tv_meet_person_apply_res)
    TextView mTvMeetPersonApplyRes;
    int src;

    @Override
    public int initContentView() {
        return R.layout.activity_feedback_meet_detail;
    }

    @Override
    public void initInjector() {
        mMeetApi = new MeetApi(mContext);
        src = getIntent().getIntExtra("src", 0);

        LogUtil.e("id = " + src);

        roomId = getIntent().getIntExtra("roomid", 0);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        if (src == 0) {
            fetchRoomDetail();
        } else {
            fillDetail((MatterMeet) getIntent().getParcelableExtra("room"));
        }
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    private void fetchRoomDetail() {
        mMeetApi.fetchMeetRoomInfo(roomId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<FeedbackDetailEntity>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<FeedbackDetailEntity> feedbackDetailEntityBaseRequestResult) {
                        fillDetail(feedbackDetailEntityBaseRequestResult.getResultMessage());
                    }
                });
    }


    private void fillDetail(FeedbackDetailEntity entity) {
        mTvPersonName.setText(entity.getUsername());
        mTvApplyTime.setText(entity.getCreated_at());
        mTvMeetName.setText(entity.getName());
        mTvMeetTime.setText(entity.getStart());
        mTvMeetRom.setText(entity.getRoom_name());
        mTvMeetPurpose.setText(entity.getDetail());
        mTvMeetApplyRes.setText(entity.getCheckname());
        loadHead(Global.BASE_URL + entity.getId_card_head_image(), mImgHeader);
        if (entity.getCheckstatus() != 2) {
            mItemApply.setVisibility(View.VISIBLE);
            loadHead(Global.BASE_URL + entity.getCheck_image(), mImgApplyHeader);
            mTvApplyPersonName.setText(entity.getCheckusername());
            mTvApplyPersonMsg.setText(entity.getCheck_feedback());
            mTvMeetPersonApplyRes.setText(entity.getCheckname());
        }
    }
    private void fillDetail(MatterMeet entity) {
        mTvPersonName.setText(entity.getUsername());
//        mTvApplyTime.setText(entity.get());
        mTvMeetName.setText(entity.getName());
        mTvMeetTime.setText(entity.getStart());
        mTvMeetRom.setText(entity.getRoom_name());
        mTvMeetPurpose.setText(entity.getDetail());
        mTvMeetApplyRes.setText(entity.getCheckstatus_name());
        loadHead(Global.BASE_URL + entity.getAvatar(), mImgHeader);
        if (entity.getCheckstatus() != 2) {
            mItemApply.setVisibility(View.VISIBLE);
            loadHead(Global.BASE_URL + entity.getCheck_image(), mImgApplyHeader);
            mTvApplyPersonName.setText(entity.getCheckname());
            mTvApplyPersonMsg.setText(entity.getCheck_feedback());
            mTvMeetPersonApplyRes.setText(entity.getCheckstatus_name());
        }

        if (entity.getCheckstatus() == 3) {
            mTvMeetPersonApplyRes.setTextColor(Color.RED);
            mTvMeetApplyRes.setTextColor(Color.RED);
        }
    }


    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }


}
